
import storeTool from './indexedStore'
import axios from 'axios'
// rgb to hex
function rgbToHex(r, g, b) {
    var hex = ((r << 16) | (g << 8) | b).toString(16)
    return '#' + new Array(Math.abs(hex.length - 7)).join('0') + hex
}
// hex to rgb
function hexToRgb(hex) {
    var rgb = []
    for (var i = 1; i < 7; i += 2) {
        rgb.push(parseInt('0x' + hex.slice(i, i + 2)))
    }
    return rgb
}
// 计算渐变过渡色
function gradient(startColor, endColor, step) {
    // 将 hex 转换为rgb
    var sColor = hexToRgb(startColor)
    var eColor = hexToRgb(endColor)
    // 计算R\G\B每一步的差值
    var rStep = (eColor[0] - sColor[0]) / step
    var gStep = (eColor[1] - sColor[1]) / step
    var bStep = (eColor[2] - sColor[2]) / step
    var gradientColorArr = []
    for (var i = 0; i < step; i++) {
        // 计算每一步的hex值
        gradientColorArr.push(
            rgbToHex(
                parseInt(rStep * i + sColor[0]),
                parseInt(gStep * i + sColor[1]),
                parseInt(bStep * i + sColor[2])
            )
        )
    }
    return gradientColorArr
}

function rotatePoint(s, t, angle) {
    var a = 0.5
    var b = 0.5
    var rx =
        a +
        (s - a) * Math.cos((angle * Math.PI) / 180) -
        (t - b) * Math.sin((angle * Math.PI) / 180)
    var ry =
        b +
        (s - a) * Math.sin((angle * Math.PI) / 180) +
        (t - b) * Math.cos((angle * Math.PI) / 180)

    return {
        s: Math.floor(rx * 1000000) / 1000000,
        t: Math.floor(ry * 1000000) / 1000000
    }
}
function normalization(distribution, max, min) {
    return (distribution - min) / (max - min)
}
function getST(position, angle, maxLon, minLon, maxLat, minLat) {
    // var pointNum = position.length
    var sts = []
    for (var i = 0; i < position.length; i = i + 3) {
        var s = this.normalization(position[i], maxLon, minLon) * 0.5 + 0.25
        var t = this.normalization(position[i + 1], maxLat, minLat) * 0.5 + 0.25
        var rst = this.rotatePoint(s, t, angle)
        sts.push(rst.s)
        sts.push(rst.t)
    }
    return sts
}

function getPoints(basin, refresh = false) {
    return new Promise(async(resolve, reject) => {
        // var url = `${mapConfig.resUrlPre}/txt/${basin}/point.txt`
        var url = `/mapData/guangdong/txt/${basin}/point.txt`
        if (!refresh) {
            storeTool.getItem(url).then(point => {
                if (point && point.lonlats && point.lonlats.length > 0 && !refresh) {
                    resolve(point)
                } else {
                    resolve(this.getPoints(basin, true))
                }
            })
            return
        }

        axios.get(url).then(resPoint => {
            var lonlats = []
            var pointRows = resPoint.data.split('\r\n')
            var maxLon = 0
            var minLon = 180
            var maxLat = 0
            var minLat = 90
            pointRows.forEach(lonlatstr => {
                var oneLonlat = lonlatstr.split(',')
                if (oneLonlat && oneLonlat.length > 1) {
                    maxLon = maxLat > Number(oneLonlat[0]) ? maxLon : Number(oneLonlat[0])
                    minLon = minLon < Number(oneLonlat[0]) ? minLon : Number(oneLonlat[0])
                    maxLat = maxLat > Number(oneLonlat[1]) ? maxLat : Number(oneLonlat[1])
                    minLat = minLat < Number(oneLonlat[1]) ? minLat : Number(oneLonlat[1])
                    lonlats.push([Number(oneLonlat[0]), Number(oneLonlat[1])])
                }
            })

            // 正方形UV
            var sub = (maxLon - minLon) - (maxLat - minLat)
            if (sub > 0) {
                maxLat = maxLat + sub / 2
                minLat = minLat - sub / 2
            } else {
                maxLon = maxLon - sub / 2
                minLon = minLon + sub / 2
            }
            maxLon = maxLon + 0.0005
            minLon = minLon - 0.0005

            if (isNaN(maxLon) || isNaN(minLon) || isNaN(maxLat) || isNaN(minLat)) {
                console.log('表坐标的最大最小值异常')
                reject()
                return
            }

            var res = { basin, lonlats, maxLon, minLon, maxLat, minLat }

            storeTool.setItem(url, res)
            resolve(res)
        })
    })
}
function getMeshs(basin, refresh = false) {
    return new Promise((resolve, reject) => {
        var url = `/mapData/guangdong/txt/${basin}/mesh.txt`
        if (!refresh) {
            storeTool.getItem(url).then(res => {
                if (res && res.pointIndexs && res.pointIndexs.length > 0 && !refresh) {
                    resolve(res)
                } else {
                    resolve(this.getMeshs(basin, true))
                }
            })
            return
        }

        axios.get(url).then(resMesh => {
            var pointIndexs = []
            var meshs = resMesh.data.split('\r\n')
            // meshs = meshs.slice(0, 10)
            meshs.forEach(item => {
                var oneRow = item.split(',')
                pointIndexs.push(oneRow)
            })
            var res = { basin, pointIndexs }
            storeTool.setItem(url, res)
            resolve(res)
        })
    })
}
export default { gradient, getPoints, getMeshs }
